// 此页面提供流程配置表单预览的功能
import React, { useState, useEffect } from 'react';
import { message } from 'antd';
import { connect } from 'dva';
import get from 'lodash/get';
import request from '@/utils/request';
import Ikon from '@/components/Ikon';
import PageLoading from '@/components/PageLoading/index';
import { dealId } from '../components/utils';
import SlickForm from '../components/SlickForm';

function Flow2({
  formId,
  formCode,
  formVersionId,
  objInstId,
  objType,
  flowId,
  orderId,
  execId,
  busiObjArgs,
  mode,
}) {
  const [initialValues, setInitialValues] = useState({});
  const [config, setConfig] = useState();
  const [ready, setReady] = useState(false);

  useEffect(() => {
    // 是否存在表单ID或者表单版本ID
    if (
      (formId && formId !== -1) ||
      (formVersionId && formVersionId !== -1) ||
      (formCode && formCode !== -1)
    ) {
      // 必须保证同时，否则表单初始值会赋值失败
      if (objInstId) {
        // 获取模板数据
        const req1 = request('pmc/pcms/formDesigner/query', {
          data: { formId, formCode, formVersionId, isTemplate: true },
        });

        // 获取模板中 元素的初始值
        const req2 = request('pmc/pcms/formInst/queryForm', {
          data: { formId, formCode, formVersionId, objInstId },
        });

        Promise.all([req1, req2]).then(result => {
          const [result1, result2] = result;
          const { success: success1, data: data1, errMessage: errMessage1 } = result1;
          const { success: success2, data: data2, errMessage: errMessage2 } = result2;

          if (success1 === true && success2 === true) {
            const {
              columnNum = 3,
              vertical = true,
              eleDriveRuleList = [],
              elementList = [],
              labelWidth = 0,
              needSubmit = true,
              needSave = false,
              needBack = false,
              submitUrl,
              saveUrl,
            } = data1;
            setReady(true);
            setConfig({
              columnNum,
              vertical,
              linkages: eleDriveRuleList,
              formSchema: dealId(elementList),
              labelWidth: labelWidth || 0,
              submitUrl,
              needSubmit,
              needSave,
              needBack,
              saveUrl,
            });

            setInitialValues(data2);
          } else {
            message.error(errMessage1 || errMessage2 || '获取数据失败');
          }
        });
      } else {
        request('pmc/pcms/formDesigner/query', {
          data: { formId, formCode, formVersionId, isTemplate: true },
        }).then(result => {
          if (result.success === true) {
            const {
              columnNum = 3,
              vertical = true,
              eleDriveRuleList = [],
              elementList = [],
              labelWidth = 0,
              needSubmit = true,
              needSave = false,
              needBack = false,
              submitUrl,
              saveUrl,
            } = result.data;
            setReady(true);
            setConfig({
              columnNum,
              vertical,
              linkages: eleDriveRuleList,
              formSchema: dealId(elementList),
              labelWidth: labelWidth || 0,
              submitUrl,
              needSubmit,
              needSave,
              needBack,
              saveUrl,
            });
          } else {
            message.error(result.errMessage || '获取数据失败');
          }
        });
      }
    } else {
      message.warning('缺少formId或formVersionId');
    }
  }, [formId, formCode, formVersionId, objInstId]);

  return (
    /* eslint-disable */
    <>
      {ready && Array.isArray(get(config, 'formSchema')) ? (
        Array.isArray(config.formSchema) && config.formSchema.length ? (
          <SlickForm
            {...config}
            mode={mode}
            extraProps={{
              formId,
              formCode,
              formVersionId,
              objInstId,
              objType,
              flowId,
              orderId,
              execId,
              busiObjArgs,
            }}
            initialValues={initialValues}
          />
        ) : (
          <Ikon status="404" subTitle="抱歉，暂无表单预览数据~" />
        )
      ) : (
        <PageLoading />
      )}
    </>
  );
}

export default connect(({ routing }) => ({
  location: routing.location,
}))(Flow2);
